﻿using System;
using System.Runtime.Remoting.Messaging;

namespace Sofire.AOP
{
    abstract class SinkBase : IMessageSink
    {
        private IMessageSink _NextSink;
        public SinkBase(IMessageSink nextSink)
        {
            this._NextSink = nextSink;
        }

        protected static CallAction GetAction(IMethodCallMessage call)
        {
            var methodName = call.MethodName;
            if(methodName == ".ctor") return CallAction.Constructor;

            var methodBase = call.MethodBase;
            if(methodBase.DeclaringType == Types.Object)
            {
                if(methodName == "FieldGetter") return CallAction.FieldGetter;

                if(methodName == "FieldSetter") return CallAction.FieldSetter;
            }

            if(methodBase.IsSpecialName)
            {
                if(methodName.StartsWith("set_")) return CallAction.PropertySetter;

                if(methodName.StartsWith("get_")) return CallAction.PropertyGetter;

                if(methodName.StartsWith("add_")) return CallAction.EventAdder;

                if(methodName.StartsWith("remove_")) return CallAction.EventRemover;
            }
            return CallAction.InvokeMethod;

        }

        #region IMessageSink 成员

        public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
        {
            //- 不对任何异步消息进行拦截
            return this._NextSink.AsyncProcessMessage(msg, replySink);
        }

        public virtual IMessage SyncProcessMessage(IMessage msg)
        {
            return this._NextSink.SyncProcessMessage(msg);
        }

        public IMessageSink NextSink
        {
            get { return this._NextSink; }
        }

        #endregion
    }
}
